var emailFormat = false;
var userFormat = false;
var pwFormat=false;
var pw2Format=false;
function showHint(value,id)
{
	if(id=="username")
	{
		if(value.length==0)
		{
			document.getElementById("userHint").innerHTML="<span>用户名不能为空</span>";
			userFormat = false;
			return;
		}
		else
		{
			var xmlhttp = new Ajax();
			var url="index.php?controller=users&action=checkDuplicate"
			var str ="username="+value;
			xmlhttp.request(url,"post", false, str, cb,emptyFunc,emptyFunc);
			return;
		}
			
	}
	if(id=="password")
	{
		if(value.length==0)
		{
			document.getElementById("pwHint").innerHTML="<span>密码不能为空</span>";		
			pwFormat = false;
		}
		else if(value.length<6)
		{
			document.getElementById("pwHint").innerHTML="<span>密码必须大于6位</span>";		
			pwFormat = false;
		}
		else
		{
			document.getElementById("pwHint").innerHTML="<span><image src=\"img/yes.gif\"></span>";
			pwFormat=true;	
		}
	}
	if(id=="password2")
	{
		var pw = document.getElementById("password").value;
		if(value != pw)
		{
			document.getElementById("pw2Hint").innerHTML="<span>两次密码输入不一致</span>";		
			pw2Format = false;
		}
		else
		{
			document.getElementById("pw2Hint").innerHTML="<image src=\"img/yes.gif\">";	
			pw2Format = true;
		}
	}
	if(id=="email")
	{
		var format = /^([a-zA-Z0-9_\.-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

		if(value.match(format))
		{
			document.getElementById("emailHint").innerHTML="<image src=\"img/yes.gif\">";
			emailFormat=true;
			
		}
		else
		{
			document.getElementById("emailHint").innerHTML="<span>请输入正确的Email格式</span>";				
			emailFormat = false;
		}
	}
	
}
function cb(response)
{
	eval(response);
}

function emptyFunc()
{
}

function dupCallback(response)
{
	if(response.result == false)
	{
		document.getElementById("userHint").innerHTML="<image src=\"img/yes.gif\">";
		userFormat=true;
	}
	else
	{
		document.getElementById("userHint").innerHTML="<span>用户名已被注册</span>";
		userFormat = false;
	}
}

function funHandleResult(response)
{
    eval(response);
 //   var user = response.user;
 //   alert(user);
}

function userRegister()
{
if(userFormat == true && pwFormat == true && pw2Format==true && emailFormat==true)
{
    var xmlhttp   = new Ajax();
    var url1       = "index.php?controller=users&action=register";
    var username  = $O("username").value; 
    var password  = $O("password").value; 
    var password2 = $O("password2").value; 
    var email     = $O("email").value;
    var postStr   = "username="+username+"&password="+password+"&password2="+password2+"&email="+email;
    var response = 'success';
    xmlhttp.request(url1, "post", false, postStr,funHandleResult,emptyFunc,emptyFunc);
 //   alert(response.user);
 //   window.location.href='login.html';
    return false;
}
else
{
	alert("输入有误，请检查");
}
}

function registerCallback(response)
{
	
	if(response.user == null)
	{
		alert("用户名已被注册，请选择其他用户名！");
	}
	else
	{
	     window.location.href='index.html';
	}
}
		
